<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use common\models\projectpaid\ProjectPaid;
use yii\widgets\ActiveForm;
use common\models\projectmoney\ProjectMoney;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel common\models\projectpaid\ProjectPaidSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', '已支付现金');
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $this->beginBlock('siderbar'); ?>
<?= $this->render('//layouts/pro-menu') ?>
<?php $this->endBlock(); ?>
<div class="project-paid-index">

    <p>
        <?= Html::a(Yii::t('app', '新增'), ['create', 'cid' => $_GET['id']], ['class' => 'btn btn-success']) ?>
        <?= Html::a(Yii::t('app', '新增账户类型'), ['/project-accounttype/create'], ['class' => 'btn btn-warning']) ?>
        <button class="btn btn-primary" id="money-summ" type="button">现金摘要</button>
        <button class="btn btn-danger" id="alldel" type="button">删除</button>
    </p>
 <?= GridView::widget([
        'id' => "myform",
        'pjax'=>false,
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
        'headerRowOptions'=>['class'=>'kartik-sheet-style'],
        'filterRowOptions'=>['class'=>'filters'],
        'panel'=>[
            'heading'=>'<h3 class="panel-title"><i class="glyphicon glyphicon-globe"></i> 数据列表</h3>',
            'type'=>'default',
            'footer'=>false
        ],
        'toolbar' => [],
        'pager'=>[
            'options'=>['class'=>'hidden']//关闭自带分页
        ],
        'persistResize'=>false,

        'columns' => [
            [
                'class'=>'kartik\grid\CheckboxColumn',
                'headerOptions'=>['class'=>'my-table'],
            ],

            'id',

            [
                'attribute' => 'time',
                'value' => function($model) {
                    return date('Y-m-d', $model->time);
                }
            ],
            'description',
            'money',
            'content',
//             'attachment',

            [
                'attribute' => 'account_type',
                'value' => function($model) {
                    return ProjectPaid::getAccount($model->account_type);
                },
            ],

            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作',
                'template' => '{view}{update}{delete}',
                'headerOptions' => ['width' => '200'],
                'buttons' => [
                    'view' => function ($url, $model, $key) {
                        return  Html::a('&nbsp;&nbsp;<span class="glyphicon glyphicon-eye-open" ><span class="classformat">查看</span></span>', $url, ['title' => '查看'] ) ;
                    },
                    'update' => function ($url, $model, $key) {
                        return Html::a('&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil"><span class="classformat">修改</span></span>', $url, ['title' => '修改'] ) ;
                    },
                    'delete' => function ($url, $model, $key) {
                        return Html::a('&nbsp;&nbsp;<span class="glyphicon glyphicon-trash" ><span class="classformat">删除</span></span>', $url, [
                            'title' => '删除',
                            'data'=>[
                                'confirm'=>'你确定要删除'.$model->description.'吗？',
                                'method'=>'post'
                            ]
                        ] ) ;
                    },
                ],
            ],
        ],
    ]); ?>
    <?= \backend\components\GoLinkPager::widget([
        'pagination' => $dataProvider->pagination,
        'go' => true,
    ]); ?>
    <div class="row-fluid" style="display : none;">
        <?php $pattern = empty(ProjectMoney::find()->where(['pr_id' => $category_id])->asArray()->one()); ?>
    	<?php $form = $pattern?ActiveForm::begin(['action' => '/project-money/create?cid=' . $category_id,'id'=>'subform']):ActiveForm::begin(['action' => '/project-money/update?cid=' . $category_id,'id'=>'subform']); ?>
		<div class="span12">
			<table class="table table-hover table-bordered">
				<thead>
					<tr>
						<th>帐户</th>
						<th>起始现金</th>
						<th>支出总计</th>
						<th>剩余现金</th>
					</tr>
				</thead>
				<tbody>
				    <?php foreach ($types as $k=>$type): ?>
					<tr>
						<td><input class="form-control" type="text" name="ProjectMoney[name][]" value="<?= $type->name ?>" data-id = "<?= $type->id ?>" readonly /></td>
						<td><input class="form-control" type="text" name="ProjectMoney[initial_cash][]" id="initial_cash<?= $type->id ?>" value="<?= empty($model_money) ? 0 : $model_money[$type['name']]['initial_cash'] ?>"/></td>
						<td><input class="form-control" type="text" name="ProjectMoney[total_expenditure][]" id="total_expenditure<?= $type->id ?>" value="<?= empty($model_money) ? 0 : $model_money[$type['name']]['total_expenditure'] ?>" readonly /></td>
						<td><input class="form-control" type="text" name="ProjectMoney[residual_cash][]" id="residual_cash<?= $type->id ?>" value="<?= empty($model_money) ? 0 : $model_money[$type['name']]['residual_cash'] ?>" readonly /></td>
					</tr>
					<?php endforeach; ?>
					<tr>
						<td><input class="form-control" type="text" name="ProjectMoney[name][]" value="总计" readonly /></td>
						<td><input class="form-control" type="text" name="ProjectMoney[initial_cash][]" id="initial_cashs" value="<?= $model_moneytot['initial_cash']; ?>" readonly /></td>
						<td><input class="form-control" type="text" name="ProjectMoney[total_expenditure][]" id="total_expenditures" value="<?= $model_moneytot['total_expenditure']; ?>" readonly /></td>
						<td><input class="form-control" type="text" name="ProjectMoney[residual_cash][]" id="residual_cashs" value="<?= $model_moneytot['residual_cash'] ?>" readonly /></td>
					</tr>  
				</tbody>
			</table>
		</div>
		 <div class="form-group">
            <?= Html::submitButton(Yii::t('app', '保存现金摘要'), ['class' => 'btn btn-success']) ?>
         </div>
		<?php ActiveForm::end(); ?>
	</div>

</div>

<?php $this->beginBlock('footer_scripts'); ?>
    <script>
		$(document).ready(function(){
			<?php foreach ($types as $vo) { 
			    $account_type = $vo->id;
			    $res = Yii::$app->db->createCommand('SELECT SUM(money) FROM project_paid WHERE account_type =' . $account_type .' AND category = '. $category_id)
			    ->queryScalar() ? Yii::$app->db->createCommand('SELECT SUM(money) FROM project_paid WHERE account_type =' . $account_type .' AND category = '. $category_id)
			    ->queryScalar() : 0.00; ?>
				$("#total_expenditure<?= $vo->id ?>").val(<?= $res ?>);
				$("#initial_cash<?= $vo->id ?>").blur(function(){
					  $("#residual_cash<?= $vo->id ?>").val($("#initial_cash<?= $vo->id ?>").val() - $("#total_expenditure<?= $vo->id ?>").val());
					  var ress = 0;				
			         <?php foreach ($types as $vo) { ?>
					    ress = parseInt(ress) + parseInt(($("#initial_cash<?= $vo->id ?>").val() == '') ? 0 : $("#initial_cash<?= $vo->id ?>").val());
						resss = ress - parseInt(($("#total_expenditures").val()));
					 <?php } ?>

					 $("#initial_cashs").val(ress);
					 $("#residual_cashs").val(resss);
				});
			<?php } ?>
			
				// 总支出 数据库求值
				$("#total_expenditures").val(<?= Yii::$app->db->createCommand('SELECT SUM(money) FROM project_paid WHERE category = ' . $category_id)
			    ->queryScalar() ?>)
			    
			    // 总计起始金额失去焦点，总计剩余金额变化  #添加函数到 blur 事件。当 <input> 字段失去焦点时发生 blur 事件
				$("#initial_cashs").blur(function(){
					  $("#residual_cashs").val($("#initial_cashs").val() - $("#total_expenditures").val());
				});

        		// 显示现金摘要
        		$("#money-summ").click(function(){
    				$(".row-fluid").toggle();
    			});
    		// 批量删除
        	binddel("<?php echo Url::to(['project-paid/alldelete']);?>");
		});
	</script>
<?php $this->endBlock(); ?>